package com.glorysoft.hlgdpda.ui.trackin.viewmodel

import androidx.databinding.ObservableArrayList
import com.glorysoft.hlgdpda.BR
import com.glorysoft.hlgdpda.R
import com.glorysoft.hlgdpda.base.BaseViewModel
import com.glorysoft.hlgdpda.entity.LotEntity
import com.glorysoft.hlgdpda.entity.ui.CallObserveBean
import com.glorysoft.hlgdpda.entity.ui.ToastMessageBean
import com.glorysoft.hlgdpda.model.TaskRepository
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.JsonParser
import me.tatarka.bindingcollectionadapter2.ItemBinding

/**
 * FileName: BatchSizeTrackInViewModel
 * Author: XJH
 * Date: 2021/10/11 14:15
 * Description:
 * History:
 */
class BatchSizeTrackInViewModel : BaseViewModel() {
    val lotList: List<String> = listOf("序号","批次号", "产品名称", "数量", "工步名称","载具ID","状态","工单号")
    private val trackInRepository = TaskRepository()

    //载具列表
    var carrierUpItems = ObservableArrayList<LotEntity>()
    var carrierUpItemsBinding =
        ItemBinding.of<LotEntity>(BR.batchItem, R.layout.item_carrier_and_position)


    /**
     * 获取批次信息
     */
    fun searchLotInfo(lot: String, eqp: String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "LOTINFOBYID")
        map.addProperty("LOTID", lot)
        map.addProperty("EQUIPMENTID", eqp)
        launchOnlyResult({
            val lotInfo = trackInRepository.getLotInfo(map)
            defUI.callObserve.value = CallObserveBean(100,0)
            addData(lotInfo)
        })
    }

    fun searchCarrierInfo(carrier: String) {
        val map = JsonObject()
        map.addProperty("CARRIERID", carrier)
        launchOnlyResult({
            val lotInfo = trackInRepository.getLotInfoByCarrier(map)
            defUI.callObserve.value = CallObserveBean(100,0)
            addData(lotInfo)
        })
    }

    private fun addData(lotInfo: LotEntity) {
        if (carrierUpItems.isEmpty()){
            lotInfo.position = (carrierUpItems.size + 1).toString()
            carrierUpItems.add(lotInfo)
            defUI.callObserve.value = CallObserveBean(200,carrierUpItems.size)
        }else{
            carrierUpItems.forEach {
                if (it.LOT.LOTID == lotInfo.LOT.LOTID){
                    defUI.toastEvent.value = ToastMessageBean("该批次已经在列表中存在！",false)
                    return
                }
            }
            if (carrierUpItems[0].LOT.WOID != lotInfo.LOT.WOID){
                defUI.toastEvent.value = ToastMessageBean("同工单下才可以批量进站！",false)
                return
            }
            lotInfo.position = (carrierUpItems.size + 1).toString()
            carrierUpItems.add(lotInfo)
            defUI.callObserve.value = CallObserveBean(200,carrierUpItems.size)
        }
    }

    fun trackIn(eqp : String) {
        val map = JsonObject()
        map.addProperty("ACTIONTYPE","DefaultTrackIn")
        map.addProperty("ISCHECKSTEPAUTHORITY",true)
        map.addProperty("EQUIPMENTID",eqp)
        var list = mutableListOf<LotEntity.LotInfoEntity>()
        carrierUpItems.forEach {
            list.add(it.LOT)
        }
        map.add("LOTLIST", JsonParser().parse(Gson().toJson(list)).asJsonArray)
        launchOnlyResult({
            trackInRepository.defaultTrackIn(map)
            defUI.toastEvent.value = ToastMessageBean("进站成功", true)
            carrierUpItems.clear()
            defUI.startActivity.call()
        })

    }

}